package com.aitaoxue.palx.ui.register;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.InjectView;

import com.aitaoxue.palx.AppManager;
import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseActivity;
import com.aitaoxue.palx.ui.LoginActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class RegisterActivity4 extends BaseActivity {

	@InjectView(R.id.alert_line_1)
	RelativeLayout alert1;
	@InjectView(R.id.alert_line_2)
	RelativeLayout alert2;
	@InjectView(R.id.alert_content_1)
	TextView alertContent1;
	@InjectView(R.id.alert_content_2)
	TextView alertContent2;
	@InjectView(R.id.btn_push_1)
	Button push1;
	@InjectView(R.id.btn_push_2)
	Button push2;

	@InjectView(R.id.big_shit)
	TextView content1;
	@InjectView(R.id.content_2)
	TextView content2;

	private CountDownTimer counter;

	private boolean isShit = false;

	@Override
	protected boolean hasBackButton() {
		return true;
	}

	@Override
	protected int getActionBarTitle() {
		return R.string.register;
	}

	@Override
	protected int getLayoutId() {
		return R.layout.activity_register_4;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_push_1:
			if (TextUtils.isEmpty(content1.getText().toString())) {
				showToast("请输入大使编号", 0, 0);
			} else {
				showWaitDialog();
				isShit = true;
				AitaoXueApi.sendchecknum2(content1.getText().toString(),
						mHandler);
			}
			break;
		case R.id.btn_push_2:
			if (isShit) {
				// 要输淘学码
				if (TextUtils.isEmpty(content2.getText())) {
					showToast("请输入淘学码", 0, 0);
				} else {
					showWaitDialog();
					AitaoXueApi.checkVerify2(content2.getText().toString(),
							mHandler2);
				}
			} else {
				// 选择是否进入引导
				gotoChoose();
			}
			break;
		default:
			break;
		}

	}

	@Override
	public void initView() {
		push1.setOnClickListener(this);
		push2.setOnClickListener(this);
		
		AppManager.getAppManager().addActivity(this);
	}

	@Override
	public void initData() {

	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {
		}

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				switch (code) {
				case "0000":
					showToast("验证码已成功发送", 0, 0);
					trimCounting();
					break;
				case "0302":
					showError1("");
					break;
				case "0312":
					showError1("校园大使不存在");
					break;
				default:
					break;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	};

	private AsyncHttpResponseHandler mHandler2 = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");

				switch (code) {
				case "0000":
					gotoChoose();
					break;
				case "0311":
					showError2("淘学码有误,请重新输入");
					break;
				default:
					break;
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	private void showError1(String error) {
		if (alert1.getVisibility() == View.GONE) {
			alert1.setVisibility(View.VISIBLE);
		}
		alertContent1.setText(error);
	}

	private void showError2(String error) {
		if (alert2.getVisibility() == View.GONE) {
			alert2.setVisibility(View.VISIBLE);
		}
		alertContent2.setText(error);
	}

	private void trimCounting() {
		push1.setEnabled(false);
		counter = new CountDownTimer(60 * 1000, 1000) {

			@Override
			public void onTick(long millisUntilFinished) {
				push1.setText(millisUntilFinished / 1000 + "");
			}

			@Override
			public void onFinish() {
				push1.setClickable(true);
				push1.setText("获取淘学码");
				counter.cancel();
			}
		};

		counter.start();
	}

	@Override
	protected void onDestroy() {
		if (counter != null) {
			counter.cancel();
			counter = null;
		}
		super.onDestroy();
	}

	private void gotoChoose() {
		AlertDialog dialog = new AlertDialog.Builder(this)
				.setMessage("是否进入引导\n不完成引导无法获得所有功能")
				.setPositiveButton("进入", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						Intent intent = new Intent(RegisterActivity4.this,
								GuideActivity.class);
						startActivity(intent);
						//finish();
						AppManager.getAppManager().finishAllActivity();
					}
				})
				.setNegativeButton("不进入",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								dialog.dismiss();
								Intent intent = new Intent(
										RegisterActivity4.this,
										LoginActivity.class);
								startActivity(intent);
								//finish();
							}
						}).create();
		dialog.show();
	}

}
